import 'package:flutter/material.dart';
import '../../utils/util.dart';
import 'controller.dart';
import 'index.dart';

class SelectionTitlesView extends StatelessWidget {
  SelectionTitlesView({
    Key? key,
    required this.titles,
    required this.complete,
    this.onTitleTap,
  }) : super(key: key);
  final List<SelectionModel> titles;
  final controller = SelectionController();
  final Function(SelectionResultModel res)? complete;
  final Function(int)? onTitleTap;
  @override
  Widget build(BuildContext context) {
    return Container(
      width: Get.width,
      alignment: Alignment.center,
      key: controller.titleKey,
      child: Wrap(
        children: titles
            .asMap()
            .map(
              (idx, value) => MapEntry(
                idx,
                SelectionTitlesItemView(
                  text: value.text,
                  isSelected: value.isSelected,
                  width: Get.width / 3,
                  onTap: () {
                    if (isZeroList(value.list)) {
                      if (onTitleTap != null) {
                        onTitleTap!(idx);
                        return;
                      }
                    }
                    controller.actionTitle(
                        index: idx, md: value, complete: complete);
                  },
                ),
              ),
            )
            .values
            .toList(),
      ),
    );
  }
}

class SelectionTitlesItemView extends StatelessWidget {
  const SelectionTitlesItemView({
    Key? key,
    required this.text,
    this.isSelected = false,
    this.width,
    this.onTap,
  }) : super(key: key);
  final String text;
  final bool isSelected;
  final double? width;
  final VoidCallback? onTap;
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: Container(
        height: 40.r,
        width: width,
        padding: EdgeInsets.symmetric(horizontal: 10.r),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              text,
              style: TextStyle(
                color: isSelected ? ColorsUtil.theme : ColorsUtil.title,
                fontSize: 13.sp,
              ),
            ),
            SizedBox(
              width: 5.r,
            ),
            Icon(
              IconUtil.arrow_down,
              size: 10.r,
              color: isSelected ? ColorsUtil.theme : ColorsUtil.title,
            ),
          ],
        ),
      ),
    );
  }
}
